package com.example.qu_tao.View;

import android.net.wifi.aware.PublishConfig;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.qu_tao.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private FabuFragment fabuFragment;
    private ShouyeFragment shouyeFragment;
    private WodeFragment wodeFragment;
    private XiaoxiFragment xiaoxiFragment;
    private XinwenFragment xinwenFragment;
    private QuTaoQuanFragment qutaoquanFragment;
    private MianjiaShouyeFragment mianjiashouyeFragment;
    private Fragment mContentFragment;
    private FragmentManager mFriendsFragment;
    private FragmentManager mFragmentManager;
    private LinearLayout buttomfabu;
    private LinearLayout buttomshouye;
    private LinearLayout buttomwode;
    private LinearLayout buttomxiaoxi;
    private LinearLayout buttomxinwen;
    private TextView xinwenText;

    // 当前模式：true为卖家模式，false为买家模式
    private boolean isSellerMode = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initFragments();
        setDefaultFragment();


    }


    private void initViews() {
        buttomfabu = findViewById(R.id.main_fabu);
        buttomshouye = findViewById(R.id.main_shouye);
        buttomwode = findViewById(R.id.main_wode);
        buttomxiaoxi = findViewById(R.id.main_xiaoxi);
        buttomxinwen = findViewById(R.id.main_xinwen);
        xinwenText = buttomxinwen.findViewById(R.id.text_xinwen);

        buttomfabu.setOnClickListener(this);
        buttomshouye.setOnClickListener(this);
        buttomwode.setOnClickListener(this);
        buttomxiaoxi.setOnClickListener(this);
        buttomxinwen.setOnClickListener(this);
    }

    private void initFragments() {
        mFragmentManager = getSupportFragmentManager();
        fabuFragment = new FabuFragment();
        shouyeFragment = new ShouyeFragment();
        wodeFragment = new WodeFragment();
        xiaoxiFragment = new XiaoxiFragment();
        xinwenFragment = new XinwenFragment();
        mianjiashouyeFragment = new MianjiaShouyeFragment();
        qutaoquanFragment = new QuTaoQuanFragment();
    }

    private void setDefaultFragment() {
        if (mFragmentManager.findFragmentById(R.id.fragment_content) == null) {
            mFragmentManager.beginTransaction()
                    .add(R.id.fragment_content, shouyeFragment)
                    .commit();
            mContentFragment = shouyeFragment;
            updateBottomNavSelection(R.id.main_shouye);
        }
    }

    @Override
    public void onClick(View v) {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();

        if (v.getId() == R.id.main_shouye) {
            if (isSellerMode) {
                replaceFragment(mianjiashouyeFragment, transaction);
            } else {
                replaceFragment(shouyeFragment, transaction);
            }
            updateBottomNavSelection(R.id.main_shouye);
        } else if (v.getId() == R.id.main_xinwen) {
            if (isSellerMode) {
                replaceFragment(qutaoquanFragment, transaction);
            } else {
                replaceFragment(xinwenFragment, transaction);
            }
            updateBottomNavSelection(R.id.main_xinwen);
        } else if (v.getId() == R.id.main_fabu) {
            replaceFragment(fabuFragment, transaction);
            updateBottomNavSelection(R.id.main_fabu);
        } else if (v.getId() == R.id.main_xiaoxi) {
            replaceFragment(xiaoxiFragment, transaction);
            updateBottomNavSelection(R.id.main_xiaoxi);
        } else if (v.getId() == R.id.main_wode) {
            replaceFragment(wodeFragment, transaction);
            updateBottomNavSelection(R.id.main_wode);
        }
    }

    private void replaceFragment(Fragment fragment, FragmentTransaction transaction) {
        if (fragment == null || fragment == mContentFragment) {
            return; // 避免重复加载相同 Fragment
        }

        // 清空回退栈，避免 Fragment 堆积
        mFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

        transaction.replace(R.id.fragment_content, fragment);
        transaction.addToBackStack(fragment.getClass().getSimpleName());
        transaction.commit();
        mContentFragment = fragment;
    }

    public void updateBottomNavSelection(int selectedTabId) {
        int[] tabIds = {R.id.main_shouye, R.id.main_xinwen, R.id.main_fabu, R.id.main_xiaoxi, R.id.main_wode};
        for (int id : tabIds) {
            LinearLayout tab = findViewById(id);
            boolean isSelected = (id == selectedTabId);
            tab.setSelected(isSelected);

            ImageView icon = (ImageView) tab.getChildAt(0);
            TextView text = (TextView) tab.getChildAt(1);
            icon.setSelected(isSelected);
            text.setSelected(isSelected);

            if (id == R.id.main_xinwen) {
                text.setText(isSellerMode ? "趣淘圈" : "趣淘新声");
            }
        }
    }

    public void switchToSellerMode(boolean isSeller) {
        this.isSellerMode = isSeller;
        updateBottomNavSelection(getCurrentSelectedTabId());
    }

    private int getCurrentSelectedTabId() {
        int[] tabIds = {R.id.main_shouye, R.id.main_xinwen, R.id.main_fabu, R.id.main_xiaoxi, R.id.main_wode};
        for (int id : tabIds) {
            if (findViewById(id).isSelected()) {
                return id;
            }
        }
        return R.id.main_shouye;
    }
}